﻿@model ChampionsLeagueBets.Web.Mvc.Controllers.ViewModels.TournamentViewModel
@using MvcContrib.UI.Grid
@{
	ViewBag.Title = "Edycja danych zawodu";
	ViewBag.Image = "~/Content/images/tournament-64.png";
}
@using (Html.BeginForm())
{
	@Html.AntiForgeryToken()

	<div id="content">
		<fieldset class="form-data">
			<legend>Dane podstawowe</legend>
			<table>
				<tr>
					<th>
						@Html.LabelFor(t => t.Tournament.Name):
					</th>
					<td>@Html.EditorFor(t => t.Tournament.Name) @Html.ValidationMessageFor(t => t.Tournament.Name)
					</td>
				</tr>
				<tr>
					<th>
						@Html.LabelFor(t => t.Tournament.StartDate):
					</th>
					<td>@Html.EditorFor(t => t.Tournament.StartDate) @Html.ValidationMessageFor(t => t.Tournament.StartDate)
					</td>
				</tr>
				<tr>
					<th>
						@Html.LabelFor(t => t.Tournament.EndDate):
					</th>
					<td>@Html.EditorFor(t => t.Tournament.EndDate) @Html.ValidationMessageFor(t => t.Tournament.EndDate)
					</td>
				</tr>
				<tr>
					<th>
						@Html.LabelFor(t => t.Tournament.Money):
					</th>
					<td>@Html.EditorFor(t => t.Tournament.Money) @Html.ValidationMessageFor(t => t.Tournament.Money)
					</td>
				</tr>
				<tr>
					<th>
						@Html.LabelFor(t => t.Tournament.UnitOfAccount):
					</th>
					<td>@Html.EditorFor(t => t.Tournament.UnitOfAccount) @Html.ValidationMessageFor(t => t.Tournament.UnitOfAccount)
					</td>
				</tr>
			</table>
		</fieldset>
		<div id="PartialToRefresh">
			@Html.Partial("_GamesList", Model.Tournament.Games.Where(g => !g.IsDeleted).ToList())
		</div>
		<a href="#" id="create-user">Dodaj</a>
	</div>

	<div class="ui-actions">
		@Html.SubmitButton("btnSubmit", "Zapisz")
		@(Html.ActionLink<TournamentsController>(c => c.Index(), "Anuluj", new { @class = "link-button" }))
	</div>
}
<div id="dialog-form" title="Dodawanie nowego meczu">
	@using (Ajax.BeginForm("Create", "Games", new AjaxOptions() { UpdateTargetId = "PartialToRefresh", OnComplete = "RefreshJavaScript" }))
 {
		@Html.AntiForgeryToken()
		@Html.Hidden("tourId", (object)Model.Tournament.Id)
	
		<fieldset class="form-data">
			<legend>Dane podstawowe</legend>
			<table>
				<tr>
					<th>
						Gospodarz:
					</th>
					<td>@Html.EditorFor(c => c.Game.TeamHost) @Html.ValidationMessageFor(c => c.Game.TeamHost)
					</td>
				</tr>
				<tr>
					<th>
						Goście:
					</th>
					<td>@Html.EditorFor(c => c.Game.TeamGuest) @Html.ValidationMessageFor(c => c.Game.TeamGuest)
					</td>
				</tr>
				<tr>
					<th>
						Data:
					</th>
					<td>@Html.EditorFor(c => c.Game.GameDate) @Html.ValidationMessageFor(c => c.Game.GameDate)
					</td>
				</tr>
			</table>
		</fieldset>

		@Html.SubmitButton("btnAddGame", "Dodaj", new { @style = "display:none" })
 }
</div>
<div id="dialog-result" title="Ustaw wynik meczu">
	@using (Ajax.BeginForm("Result", "Games", new AjaxOptions() { UpdateTargetId = "PartialToRefresh", OnComplete = "RefreshJavaScript" }))
 {
		@Html.AntiForgeryToken()
		@Html.Hidden("id")
	
		<fieldset class="form-data">
			<legend>Wynik meczu</legend>
			<table>
				<tr>
					<th>
						Gospodarz:
					</th>
					<td>@Html.TextBox("resultTeamHost")
					</td>
				</tr>
				<tr>
					<th>
						Goście:
					</th>
					<td>@Html.TextBox("resultTeamGuest")
					</td>
				</tr>
			</table>
		</fieldset>

		@Html.SubmitButton("btnGameResult", "Zapisz", new { @style = "display:none" })
 }
</div>
<div id="dialog-confirm" title="Usunąć mecz?">
	<p>
		<span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>
		Dane meczu zostaną usuniętę. Kontynuować?</p>
</div>
<script language="javascript" type="text/javascript">

	$(document).ready(function () {

		$("#dialog-form").dialog({
			autoOpen: false,
			height: 300,
			width: 700,
			modal: true,
			buttons: {
				"Dodaj": function () {
					$("#btnAddGame").click();
					$(this).dialog("close");
				},
				"Anuluj": function () {
					$(this).dialog("close");
				}
			}
		});

		$("#dialog-result").dialog({
			autoOpen: false,
			height: 240,
			width: 700,
			modal: true,
			buttons: {
				"Ustaw": function () {
					$("#btnGameResult").click();
					$(this).dialog("close");
				},
				"Anuluj": function () {
					$(this).dialog("close");
				}
			}
		});

		$("#dialog-confirm").dialog({
			autoOpen: false,
			modal: true,
			resizable: false,
			height: 180
		});

		$("#create-user")
			.click(function () {
				$("#dialog-form").dialog("open");
			});

		$('#resultTeamHost').numeric();
		$('#resultTeamGuest').numeric();
		$('#Tournament_Money').numeric();

		RefreshJavaScript();
	});

	function RefreshJavaScript() {

		$(".deleteLink").click(function (e) {
			e.preventDefault();
			var targetUrl = $(this).attr("href");

			$("#dialog-confirm").dialog({
				buttons: {
					"Usuń": function () {
						//window.location.href = targetUrl;
						$.ajax({
							url: targetUrl,
							success: function (html) {
								$("#PartialToRefresh").html(html);
								SucessDelete();
								RefreshJavaScript();
							}
						});
						$(this).dialog("close");
					},
					"Anuluj": function () {
						$(this).dialog("close");
					}
				}
			});

			$("#dialog-confirm").dialog("open");
		});

		$(".setResultLink")
			.click(function (e) {
				e.preventDefault();

				var lists = $(this).attr("href").split('/');
				var id = lists[lists.length - 1].split('?')[0];
				$("#id").val(id);

				$("#dialog-result").dialog("open");
			});
		}

		function SucessDelete() {
			$("#page-message-contener").html("<div id='page-message'><div id='page-message-success'>Zawody zostały usunięte.</div></div>");
			window.ShowPageMessage();
		}
		
</script>
